home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / texinfo / texi2dvi < prev    next >
Text File  |  1992-05-13  |  10KB  |  284 lines

  1. #!/bin/sh
  2. # texi2dvi -- smartly produce DVI files from texinfo sources
  3. #
  4. # Copyright (C) 1992 Free Software Foundation.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, you can either send email to this
  18. # program's author (see below) or write to:
  19. #
  20. #              Free Software Foundation, Inc.
  21. #              675 Mass Ave.
  22. #              Cambridge, MA 02139, USA. 
  23. #
  24. # Please send bug reports, etc. to friedman@prep.ai.mit.edu
  25. # If possible, please send a copy of the output of the script called with
  26. # the `--debug' option when making a bug report. 
  27. #
  28. # Version 0.2
  29. #
  30.  
  31. # Please note that in the interest of general portability, some common
  32. # bourne shell constructs were avoided because they weren't guaranteed to
  33. # be available in some earlier implementations.  I've tried to make this as
  34. # portable as possible. 
  35. #
  36. # Among the more interesting lossages I noticed with some bourne shells
  37. # are:
  38. #       1) Some don't have an `unset' builtin 
  39. #       2) In some implementations the `shift' builtin can't take a
  40. #          numerical argument. 
  41.  
  42. progname=`basename $0`
  43.  
  44. usage="Usage: ${progname} {-d} {-f} {-h} [file1] {file2} {...}
  45.        {--debug} {--force} {--help}
  46.  
  47.    Options in braces are optional.  Those in brackets are required. 
  48. "
  49.  
  50. if [ $# -eq 0 ]; then
  51.    echo "${usage}" 1>&2;
  52.    exit 1
  53. fi
  54.  
  55. backup_extension=".bak"
  56. texindex="texindex"
  57. tex="tex"
  58. bq="\`"  # To prevent hairy quoting and escaping later.
  59. eq="'"
  60. orig_pwd="`pwd`"
  61.  
  62. if [ "z${TEXINDEX}" != "z" ]; then
  63.    texindex="${TEXINDEX}"
  64. fi
  65.  
  66. if [ "z${TEX}" != "z" ]; then
  67.    tex="${TEX}"
  68. fi
  69.  
  70. # Parse command line options
  71.  
  72. # "unset" option variables to make sure they weren't accidentally
  73. # exported
  74. force=""
  75. debug=""
  76.  
  77. # If you add new commands be sure to change the wildcards below to make
  78. # sure they are unambiguous (i.e. only match one possible long option)
  79. # Be sure to show at least one instance of the full long option name to
  80. # document what the long option is canonically called. 
  81. while [ $# -gt 0 ]; do
  82.    case z$1 in
  83.       z-d | z--debug | z--d* )
  84.          debug="t"
  85.          shift
  86.         ;;
  87.       z-f | z--force | z--f* )
  88.          force="t"
  89.          shift
  90.         ;;
  91.       z-h | z--help | z--h* )
  92.          echo "${usage}" 1>&2
  93.          exit 1
  94.         ;;
  95.       z-- )
  96.          shift
  97.          break 
  98.         ;;
  99.       z-* )
  100.          echo "${progname}: ${bq}${1}${eq} is not a valid option." 1>&2
  101.          echo "" 1>&2
  102.          echo "${usage}" 1>&2
  103.          exit 1
  104.         ;;
  105.       * )
  106.          break 
  107.         ;;
  108.    esac
  109. done
  110.  
  111. # See if there are any command line args left (which will be interpreted as
  112. # filename arguments)
  113. if [ $# -eq 0 ]; then
  114.    echo "${progname}: at least one file name is required as an argument." 1>&2
  115.    echo "" 1>&2
  116.    echo "${usage}" 1>&2
  117.    exit 1
  118. fi
  119.  
  120. if [ "z${debug}" = "zt" ]; then
  121.    set -x
  122. fi
  123.  
  124. # Texify files
  125. for command_line_filename in "$@" ; do
  126.    # Always chdir to original working directory so that all relative
  127.    # pathnames in command-line arguments are evaluated with respect to the
  128.    # parent environment, rather than the working directory of the last file
  129.    # (which might not have been the same). 
  130.    cd "${orig_pwd}"
  131.  
  132.    # Roughly equivalent to `dirname ...`, but more portable
  133.    directory="`echo ${command_line_filename} | sed 's/\/[^\/]*$//'`"
  134.    file="`basename ${command_line_filename}`"
  135.    # Strip off the last extension part (probably .texinfo or .texi)
  136.    filename_noext="`echo ${file} | sed 's/\.[^.]*$//'`"
  137.  
  138.    # "Unset" variables that might have values from previous iterations and
  139.    # which won't be completely reset later.
  140.    definite_index_files=""
  141.  
  142.    # If directory and file are the same, then it's probably because there's
  143.    # no pathname component.  Set dirname to `.', the current directory.
  144.    if [ "z${directory}" = "z${file}" ]; then
  145.       directory="."
  146.    fi
  147.  
  148.    # Change to directory where file is located. 
  149.    cd "${directory}"
  150.    if [ $? -ne 0 ]; then
  151.       echo "${progname}: could not chdir to ${bq}${directory}${eq}" 1>&2
  152.       continue
  153.    fi
  154.  
  155.    # See if file exists here.  If it doesn't we're in trouble since, even
  156.    # though the user may be able to reenter a valid filename at the tex
  157.    # prompt (assuming they're attending the terminal), this script won't be
  158.    # able to find the right index files and so forth.
  159.    if [ ! -r "${file}" ]; then
  160.       echo "${progname}: ${file}: No such file or permission denied." 1>&2
  161.       continue; 
  162.    fi
  163.  
  164.    # If dvi file is newer than the texinfo source, don't do anything
  165.    # (unless --force option was specified)
  166.    if [ ! "${force}" ]; then
  167.       # List file.dvi first, just in case mtimes are identical and ls -t
  168.       # sorts stably. 
  169.       filename_newest="`set -$- \`ls -t \"${filename_noext}.dvi\" \"${file}\" 2> /dev/null\`; echo \"\$1\"`"
  170.       if [ "${filename_newest}" = "${filename_noext}.dvi" ]; then
  171.          echo "${progname}: ${bq}${filename_noext}.dvi${eq} is up to date." 1>&2
  172.          continue
  173.       fi
  174.    fi
  175.  
  176.    # Find all files having root filename with a two-letter extension,
  177.    # determine whether they're really index files, and save them.  Foo.aux
  178.    # is actually the cross-references file, but we need to keep track of
  179.    # that too.
  180.    possible_index_files="`eval echo ${filename_noext}.?? ${filename_noext}.aux`"
  181.    for this_file in ${possible_index_files} ; do
  182.       # If file is empty, forget it.  
  183.       if [ ! -s "${this_file}" ]; then
  184.          continue;
  185.       fi
  186.  
  187.       # Examine first character of file.  If it's not a backslash or
  188.       # single quote, then it's definitely not an index or xref file.
  189.       first_character="`sed -n '1s/^\(.\).*$/\1/p;q' ${this_file}`"
  190.       if [ "${first_character}" = "\\" -o "${first_character}" = "'" ]; then
  191.          definite_index_files="${definite_index_files} ${this_file}"
  192.       fi
  193.    done
  194.    orig_index_files="${definite_index_files}"
  195.    orig_index_files_sans_aux="`echo ${definite_index_files} \
  196.                                 | sed 's/'${filename_noext}'\.aux//;
  197.                                        s/^[ ]*//;s/[ ]*$//;'`"
  198.  
  199.    # Now save copies of original index files so we have some means of
  200.    # comparison later. 
  201.    for index_file_to_save in ${orig_index_files} ; do
  202.        cp "${index_file_to_save}" "${index_file_to_save}${backup_extension}"
  203.    done
  204.  
  205.    # Run texindex on current index files.  If they already exist, and
  206.    # after running TeX a first time the index files don't change, then
  207.    # there's no reason to run TeX again.  But we won't know that if the
  208.    # index files are out of date or nonexistent.
  209.    if [ "${orig_index_files_sans_aux}" ]; then
  210.       ${texindex} ${orig_index_files_sans_aux}
  211.    fi
  212.  
  213.    if ${tex} ${file} ; then        # TeX run first time
  214.       definite_index_files=""
  215.       # Get list of new index files
  216.       possible_index_files="`eval echo ${filename_noext}.?? ${filename_noext}.aux`"
  217.       for this_file in ${possible_index_files} ; do
  218.          # If file is empty, forget it.
  219.          if [ ! -s ${this_file} ]; then
  220.             continue;
  221.          fi
  222.  
  223.          # Examine first character of file.  If it's not a backslash or
  224.          # single quote, then it's definitely not an index or xref file.
  225.          first_character="`sed -n '1s/^\(.\).*$/\1/p;q' ${this_file}`"
  226.          if [ "${first_character}" = "\\" -o "${first_character}" = "'" ]; then
  227.             definite_index_files="${definite_index_files} ${this_file}"
  228.          fi
  229.       done
  230.       new_index_files="${definite_index_files}"
  231.       new_index_files_sans_aux="`echo ${definite_index_files} \
  232.                                   | sed 's/'${filename_noext}'\.aux//;
  233.                                          s/^[ ]*//;s/[ ]*$//;'`"
  234.  
  235.       # If old and new list don't at least have the same file list, then one
  236.       # file or another has definitely changed.  
  237.       if [ "${orig_index_files}" != "${new_index_files}" ]; then
  238.          index_files_changed_p=t
  239.       else
  240.          # File list is the same.  We must compare each file until we find a
  241.          # difference.  
  242.          index_files_changed_p=""
  243.          for file in ${new_index_files} ; do
  244.             # cmp -s will return nonzero exit status if files differ. 
  245.             cmp -s "${file}" "${file}${backup_extension}"
  246.             if [ $? -ne 0 ] ; then
  247.                # We only need to keep comparing until we find *one* that
  248.                # differs, because we'll have to run texindex & tex no
  249.                # matter what. 
  250.                index_files_changed_p=t
  251.                break
  252.             fi
  253.          done
  254.       fi
  255.  
  256.       # If index files have changed since TeX has been run, or if the aux
  257.       # file wasn't present originally, run texindex and TeX again.
  258.       if [ "${index_files_changed_p}" ] ; then
  259.          retval=0
  260.          if [ "${new_index_files_sans_aux}" ]; then
  261.             ${texindex} ${new_index_files_sans_aux} 
  262.             retval=$?
  263.          fi
  264.          if [ ${retval} -eq 0 ]; then
  265.             ${tex} "${file}"
  266.          fi
  267.       fi
  268.    fi
  269.  
  270.    # Generate list of files to delete, then call rm once with the entire
  271.    # list.  This is significantly faster than multiple executions of rm. 
  272.    file_list=""
  273.    for file in ${orig_index_files} ; do
  274.        file_list="${file_list} ${file}${backup_extension}"
  275.    done
  276.    if [ "${file_list}" ]; then
  277.       rm -f ${file_list}
  278.    fi
  279. done
  280.  
  281. #
  282. # eof
  283. #
  284.